package git.soulbgm.common;

import git.soulbgm.cache.enumeration.TimeLevel;
import git.soulbgm.utils.DateUtil;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

/**
 * 常量
 *
 * @author SoulBGM
 * @date 2023-04-02
 */
public class Constants {

    /**
     * 产生数据包接收序列的Kafka主题
     */
    public final static String TOPIC_PACKET_RECEIVE_SEQUENCE = "packet_receive_sequence";

    /**
     * 产生数据段摘要信息的Kafka主题
     */
    public final static String TOPIC_DATA_SEGMENT_SUMMARY = "data_segment_summary";

    /**
     * 产生数据段内容摘要信息的Kafka主题
     */
    public final static String TOPIC_DATA_SEGMENT_CONTENT_SUMMARY = "data_segment_content_summary";

    public final static String GN_RECEIVE_DATA = "receiveData";
    public final static String GN_PROCESSING_INFO = "processingInfo";
    public final static String GN_STANDING = "standing";

    /**
     * 偏移时间的步长
     */
    private final static Map<TimeLevel, Integer> OFFSET_STEP_LENGTH = new HashMap<>();

    static {
        OFFSET_STEP_LENGTH.put(TimeLevel.SECONDS, 1);
        OFFSET_STEP_LENGTH.put(TimeLevel.MINUTE, 1);
        OFFSET_STEP_LENGTH.put(TimeLevel.HOUR, 1);
        OFFSET_STEP_LENGTH.put(TimeLevel.DAY, 1);
        OFFSET_STEP_LENGTH.put(TimeLevel.WEEK, 1);
        OFFSET_STEP_LENGTH.put(TimeLevel.MONTH, 1);
        OFFSET_STEP_LENGTH.put(TimeLevel.YEAR, 1);
    }

    /**
     * 获取存储入库的开始时间和结束时间
     *
     * @param level 时间等级
     * @return {@link long[]}
     */
    public static long[] getStartTimeAndEndTime(TimeLevel level) {
        LocalDateTime nowDateTime = DateUtil.getDateTime();
        return getStartTimeAndEndTime(level, nowDateTime);
    }

    /**
     * 存储开始时间和结束时间
     *
     * @param level    时间等级
     * @param dateTime Calendar对象
     * @return {@link long[]}
     */
    public static long[] getStartTimeAndEndTime(TimeLevel level, LocalDateTime dateTime) {
        String unit = getUnit(level);
        LocalDateTime endDateTime = DateUtil.getStartDateTime(dateTime, unit);
        long[] time = new long[2];
        time[1] = DateUtil.getTimestamp(endDateTime) - 1;
        time[0] = DateUtil.getTimestamp(DateUtil.offsetDate(endDateTime, unit, -OFFSET_STEP_LENGTH.get(level)));
        return time;
    }

    /**
     * 判断是否执行某个时间段的统计
     *
     * @param startTimeAndEndTime 开始时间和结束时间
     * @param level               时间等级
     */
    public static long[] getStatisticsTime(long[] startTimeAndEndTime, TimeLevel level) {
        String calendarField = getUnit(level);
        long startTime = startTimeAndEndTime[0];
        long endTime = startTimeAndEndTime[1];
        LocalDateTime endDateTime = DateUtil.getEndDateTime(DateUtil.getDateTime(endTime), calendarField);
        if (DateUtil.getTimestamp(endDateTime) == endTime) {
            LocalDateTime startDateTime = DateUtil.getStartDateTime(DateUtil.getDateTime(startTime), calendarField);
            return new long[]{DateUtil.getTimestamp(startDateTime), endTime};
        }
        return new long[0];
    }

    /**
     * 将传入的field转为Calendar的field
     *
     * @param level 时间等级
     * @return int
     */
    public static String getUnit(TimeLevel level) {
        switch (level) {
            case SECONDS:
                return ChronoUnit.SECONDS.toString();
            case HOUR:
                return ChronoUnit.HOURS.toString();
            case DAY:
                return ChronoUnit.DAYS.toString();
            case WEEK:
                return ChronoUnit.WEEKS.toString();
            case MONTH:
                return ChronoUnit.MONTHS.toString();
            case QUARTER:
                return DateUtil.UNIT_QUARTERS;
            case YEAR:
                return ChronoUnit.YEARS.toString();
            default:
                return ChronoUnit.MINUTES.toString();
        }
    }

}
